package com.example.muasic.activity;

import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Point;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AccelerateInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.example.muasic.R;

import com.example.muasic.adapter.MyFragmentPagerAdapter;
import com.example.muasic.adapter.TextPagerAdapter;
import com.example.muasic.provider.MusicSqLiteHelper;
import com.example.muasic.service.IService;
import com.example.muasic.service.MusicService;

import java.io.Serializable;
import java.lang.reflect.Field;

public class IntroActivity extends AppCompatActivity {

    private static IService iService;
    private ServiceConnection conn;
    private MusicSqLiteHelper musicSqLiteHelper;

    public static boolean SHOW_TWO_ANIM = true;//第二个界面是否展示动画 网易云音乐 3->2时 2没展示动画效果
    com.example.muasic.activity.MyInterceptViewPager mTextPager;//文字
    com.example.muasic.activity.MyInterceptViewPager mImageViewPager;//图片
    RelativeLayout mTouchLayout;//点击分发
    ImageView mIndicatorOne, mIndicatorTwo, mIndicatorThree;
    int pageIndex = 0;
    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intro);

       /* // 开启服务
        Intent intent = new Intent(this, MusicService.class);
        startService(intent);
        conn = new ServiceConnection(){
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                //6. 获取IBinder对象, 以此调用暴露的函数
                iService = (IService) service;
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
            }
        };

        //绑定服务

        bindService(intent, conn, BIND_AUTO_CREATE);*/

        mTextPager =(com.example.muasic.activity.MyInterceptViewPager)findViewById(R.id.main_text_pager);
        mImageViewPager =(com.example.muasic.activity.MyInterceptViewPager)findViewById(R.id.main_image_pager);
        mTouchLayout = (RelativeLayout)findViewById(R.id.main_touch_layout);
        mIndicatorOne =(ImageView) findViewById(R.id.main_indicator_one);
        mIndicatorTwo =(ImageView) findViewById(R.id.main_indicator_two);
        mIndicatorThree =(ImageView) findViewById(R.id.main_indicator_three);

        try {
            Field field = ViewPager.class.getDeclaredField("mScroller");//反射
            field.setAccessible(true);
            FixedSpeedScroller scrollerText = new FixedSpeedScroller(this, new AccelerateInterpolator());
            field.set(mTextPager, scrollerText);
            scrollerText.setmDuration(350);
        } catch (Exception e) {

        }
        MyFragmentPagerAdapter fragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
        TextPagerAdapter textPagerAdapter = new TextPagerAdapter();
        mTextPager.setAdapter(textPagerAdapter);
        mImageViewPager.setAdapter(fragmentPagerAdapter);
        mImageViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                pageIndex = i;
                mIndicatorOne.setImageDrawable(getResources().getDrawable(R.drawable.circle_gray));
                mIndicatorTwo.setImageDrawable(getResources().getDrawable(R.drawable.circle_gray));
                mIndicatorThree.setImageDrawable(getResources().getDrawable(R.drawable.circle_gray));
                switch (i){
                    case 0:
                        SHOW_TWO_ANIM = true;
                        mIndicatorOne.setImageDrawable(getResources().getDrawable(R.drawable.circle_main));
                        break;
                    case 1:
                        mIndicatorTwo.setImageDrawable(getResources().getDrawable(R.drawable.circle_main));
                        break;
                    case 2:
                        SHOW_TWO_ANIM = false;
                        mIndicatorThree.setImageDrawable(getResources().getDrawable(R.drawable.circle_main));
                        break;
                }

            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        //点击分发 实现两个viewpager的联动，根据横向滑动距离和方向判断是否应该翻页
        mTouchLayout.setOnTouchListener(new View.OnTouchListener() {
            float startX;
            float startY;//没有用到
            float endX;
            float endY;//没有用到
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        startX = event.getX();
                        startY = event.getY();
                        break;
                    case MotionEvent.ACTION_UP:
                        endX = event.getX();
                        endY = event.getY();
                        WindowManager windowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
                        Point size = new Point();
                        windowManager.getDefaultDisplay().getSize(size);
                        int width = size.x;
                        if (startX - endX >= (width / 8)){// startX - endX 大于0 且大于宽的1/8 可以往后翻页
                            if (pageIndex == 0){
                                mImageViewPager.setCurrentItem(1);
                                mTextPager.setCurrentItem(1, true);
                            }else if (pageIndex == 1){
                                mImageViewPager.setCurrentItem(2);
                                mTextPager.setCurrentItem(2, true);
                            }
                        }else if (endX - startX  >= (width / 8)){ // endX - startX   大于0 且大于宽的1/8 可以往前翻页
                            if (pageIndex == 2){
                                mImageViewPager.setCurrentItem(1);
                                mTextPager.setCurrentItem(1, true);
                            }else if (pageIndex == 1){
                                mImageViewPager.setCurrentItem(0);
                                mTextPager.setCurrentItem(0, true);
                            }
                        }

                        break;
                }
                return true;
            }
        });
    }

    public void onLogin(View view) {
        //        Toast.makeText(this, "登录/注册", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(IntroActivity.this, com.example.muasic.activity.MainActivity.class);
        intent.putExtra("recode",1);
        this.startActivity(intent);
    }

    public void onMain(View view) {
        //        Toast.makeText(this, "立即体验", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(IntroActivity.this, com.example.muasic.activity.MainActivity.class);
       /* Bundle bundle = new Bundle();
        int pos = iService.callgetNowMusic();
        bundle.putInt("first_send",pos);
        bundle.putSerializable("musiclist", (Serializable) musicList);
        intent.putExtras(bundle);*/
        intent.putExtra("recode",0);
        this.startActivity(intent);
    }
}

